import { useEffect, useRef, useState } from 'react'

// 倒计时 Hook：返回触发函数、剩余秒数和运行状态
function useDao(count = 60){
   const [num, setNum] = useState(count)
   let timer = useRef(null)
   const [flag, setFlag] = useState(false)
   const fn = ()=>{
     setFlag(true)
     timer.current = setInterval(()=>{
        setNum((c)=>c-1)
     },1000)
   }
   useEffect(()=>{
     if(num<=0){
       clearInterval(timer.current)
       setFlag(false)
       setNum(count)
     }
   },[ num,fn ])
   return [fn,num,flag]
} 
  

export default useDao